// Pornhub.js for PSPTube
// Made By JK108

function Pornhub_CheckURL( url, option )
{
	if (url.match("http://www.pornhub.com/") == "http://www.pornhub.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Pornhub URL
*/
function Pornhub_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/addVariable\(\"options\", \"(.*?)\"/);
	var contents = GetContents(PSPTube.decodeURI(video_url[1]));
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\<flv_url\>(.*?)\<\/flv_url\>/);
	var video_url = video_url[1];
	return video_url;
}

function Pornhub_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.pornhub.com/video/search?search=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.pornhub.com/video/search?search=" + keyword.replace(/ /g, "+") + "&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<p class=\"last\">Broaden your search.<br />") == "<p class=\"last\">Broaden your search.<br />"){ result.total = 0; return result; }
	var pos = contents.indexOf("</strong> of <strong>");
	contents = contents.substring(pos + 21, contents.length);
	pos = contents.indexOf("</strong>");
	var total = contents.substring(0, pos).replace(/,/g, "");
	result.total = total;


	while(1){
	pos = contents.indexOf("<a href = \"http://www.pornhub.com/view_video.php?viewkey");

	if(pos != -1){
		//Get URL
		contents = contents.substring(pos + 11, contents.length);
		pos = contents.indexOf("\" target=");
		var url = contents.substring(0, pos);
		//Get Thumbnail
		pos = contents.indexOf("<img src=\"");
		contents = contents.substring(pos + 10, contents.length);
		pos = contents.indexOf("\" title=");
		var thumb_url = contents.substring(0, pos);
		//Title
		contents = contents.substring(pos + 9, contents.length);
		pos = contents.indexOf("\" alt=");
		var title = ImproveText(contents.substring(0, pos));
		//Length
		pos = contents.indexOf("<div class=\"box-left\">");
		contents = contents.substring(pos + 22, contents.length);
		pos = contents.indexOf("<br");
		var length = contents.substring(0, pos);
		var seconds = TimestringToSeconds(length);
		//Description
		contents = contents.substring(pos + 15, contents.length);
		pos = contents.indexOf("     </div>");
		var description = "Added " + ImproveText(contents.substring(0, pos));
		//Views
		pos = contents.indexOf("<br />");
		contents = contents.substring(pos + 12, contents.length);
		pos = contents.indexOf(" views");
		var views = parseInt(contents.substring(0, pos).replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = seconds;
		info.Description = description;
		info.ViewCount = views;
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Pornhub = new Object();
Pornhub.Name          = "Pornhub";		/* Service name */
Pornhub.Description   = "Pornhub";		/* Service description */
Pornhub.SearchDesc    = "Pornhub";		/* Service search screen description */
Pornhub.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Pornhub.CheckURL      = Pornhub_CheckURL;	/* CheckURL function */
Pornhub.GetURL        = Pornhub_GetURL;		/* GetURL function */
Pornhub.Search        = Pornhub_Search;		/* Search function */

// add Pornhub to site list
SiteList.push( Pornhub );
